#ifndef _ParamHandler_H_
#define _ParamHandler_H_
#include <iostream>
#include <string>
#include <map>
#include "../Utils/JsonUtil.h"
#include "../Core/SendingMsgDealer.h"
#define HOST "127.0.0.1"
using namespace std;

class ParamHandler{
protected:
	string operation;
	SendingMsgDealer *sender;
public:
	void setOperation(const string &op);
	virtual string handleRequest(map<string,string> &param)=0;
};

class NegotiationParamHandler:public ParamHandler{
public:
	string handleRequest(map<string,string> &param) override;
};

class EncryptParamHandler:public ParamHandler{
public:
	string handleRequest(map<string,string> &param) override;
};

class ARQParamHandler:public ParamHandler{
public:
	string handleRequest(map<string,string> &param) override;
};

class HashParamHandler:public ParamHandler{
public:
	string handleRequest(map<string,string> &param) override;
};

class DefParamHandler:public ParamHandler{
public:
	string handleRequest(map<string,string> &param) override;
};

//function defination of ParamHandler
void ParamHandler::setOperation(const string &op)
{
	this->operation=op;
}

//function defination of NegotiationParamHandler
string NegotiationParamHandler:: handleRequest(map<string,string> &param) 
{
	string type=param["type"];
	string method=param["method"];
	string message="";
	if(type=="authentication")
	{
		if(method=="1-RTT")
		{
			sender=new SendingMsgDealer(HOST);
			sender->sendMsg("negotiation_One");
			if(sender->receiveMsg()=="OK"){
				message=sender->oneRTTNegotiate("");
			}
			delete sender;
		}
		else if(method=="0-RTT")
		{
			sender=new SendingMsgDealer(HOST);
			sender->sendMsg("negotiation_Zero");
			message=sender->zeroRTTNegotiate("app_data");
			delete sender;
		}
		else{
			message="please check your param!";
		}
	}
	else if(type=="encrypt")
	{
		string model=param["model"];
		string data=param["data"];
		map<string,string> param_map;
		param_map.insert(make_pair("method",method));
		param_map.insert(make_pair("model",model));
		param_map.insert(make_pair("data",data));
		sender=new SendingMsgDealer(HOST);
		sender->sendMsg("negotiation_Encrypt");
		if(sender->receiveMsg()=="OK"){
			message=sender->encryptNegotiate(param_map);
		}
		delete sender;
	}
	else{
		message="please check your param!";
	}
	return  JsonUtil::get_response_json(message,true);
}

//function defination of EncryptParamHandler
string EncryptParamHandler:: handleRequest(map<string,string> &param) 
{
	string message="encrypt Ok ,time :220ms,using method: AES";
	return JsonUtil::get_response_json(message,true);
}

//function defination of ARQParamHandler
string ARQParamHandler:: handleRequest(map<string,string> &param) 
{
	string message="ARQ times:5 ,total testing time :220ms";
	return JsonUtil::get_response_json(message,true);
}

//function defination of HashParamHandler
string HashParamHandler:: handleRequest(map<string,string> &param) 
{
	string method=param["method"];
	sender=new SendingMsgDealer(HOST);
	string result;
	result=sender->hash(param);
	string message="vertify Ok ,time :220ms,using method: "+method+" hashing result:"+result;
	delete sender;
	return JsonUtil::get_response_json(message,true);
}
//function defination of DefParamHandler
string DefParamHandler:: handleRequest(map<string,string> &param) 
{
	string message="scaning Ok ,time :220ms";
	return JsonUtil::get_response_json(message,true);
}

#endif